﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Web;
using System.Web.Mvc;

namespace IOA.MES.Web.Controllers
{
    public class CraftsController : Controller
    {
        #region 工艺配置
        /// <summary>
        /// 订单工艺管理
        /// </summary>
        public ActionResult OrderCraft()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            ViewBag.WorkOrderStatusEnums = new CommonMgr().QueryEnumTypes(typeof(ProduceStatus));
            return View();
        }

        /// <summary>
        /// 工单工艺管理
        /// </summary>
        public ActionResult WorkOrderCraft()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            ViewBag.WorkOrderStatusEnums = new CommonMgr().QueryEnumTypes(typeof(ProduceStatus));
            return View();
        }

        /// <summary>
        /// 查询订单工艺
        /// </summary>
        [HttpPost]
        public JsonResult QueryCrafts(int page, int limit, string orderNo)
        {
            var total = 0;
            var configs = new ViaSiteConfigMgr().QueryViaSiteConfigs(page, limit, out total, orderNo);
            return Json(new ResultJson<object>(configs, total));
        }

        /// <summary>
        /// 编辑生产工艺
        /// </summary>
        public ActionResult EditViaSiteConfig(string orderNo, string workOrderNo = "")
        {
            ViewBag.OrderNo = orderNo;
            ViewBag.WorkOrderNo = workOrderNo;
            var config = new ViaSiteConfigMgr().GetConfig(orderNo, workOrderNo);
            return View(config);
        }
        #endregion

        /// <summary>
        /// 生产工单
        /// </summary>
        public ActionResult WorkOrderMgr()
        {
            ViewBag.Workshops = new WorkshopMgr().QueryUserWorkshops(BaseEnv.UserId);
            ViewBag.WorkOrderStatusEnums = new CommonMgr().QueryEnumTypes(typeof(ProduceStatus));
            return View();
        }

        #region 参数配置核查 
        public ActionResult ParamCheckMgr(string orderNo, string workOrderNo)
        {
            ViewBag.WorkOrderNo = workOrderNo;
            var workOrder = new WorkOrderMgr().GetWorkOrder(workOrderNo);
            ViewBag.Sites = new CommonMgr().QueryEnumTypes(typeof(SiteType));
            ViewBag.Config = new ViaSiteConfigMgr().GetConfig(orderNo, workOrderNo);
            var total = 0;
            ViewBag.StandardPieces = new StandardPieceMgr().QueryPieces(1, 0, out total, "", CommonStatus.Normal);
            ViewBag.WorkOrderPiece = new WorkOrderMgr().GetStandardPiece("", workOrderNo);
            ViewBag.WorkOrderBinningSchema = new BinningSchemeMgr().GetWorkorderBinningSchema(orderNo, workOrderNo);
            ViewBag.Schemas = new BinningSchemeMgr().Query(1, 0, out total, workOrder.WorkshopCode, "");
            ViewBag.PackCondition = new PackConditionMgr().GetPackCondition("", workOrderNo, QLevelCondition.Q1);
            ViewBag.CabinetCondition = new CabinetConditionMgr().GetCabinetCondition("", workOrderNo, QLevelCondition.Q1);
            ViewBag.Label = new LabelMgr().GetWorkOrderLabel(orderNo, workOrderNo);
            return View();
        }
        #endregion
    }
}
